package com.service.Impl;

import com.mapper.SysCityMapper;
import com.mapper.SysDictionaryMapper;

import com.pojo.entity.SysCity;
import com.pojo.entity.SysDictionary;
import com.service.SysDictionaryService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.sql.Time;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("SysDictionaryService")
public class SysDictionaryServiceImpl implements SysDictionaryService {
    @Resource(name = "jsonRedisTemplate")
    RedisTemplate redisTemplate;
    @Autowired
    SysDictionaryMapper sysDictionaryMapper;
    @Autowired
    SysCityMapper sysCityMapper;
    Map<String, Map<String, String>> data = new HashMap<>();

    @Override
    @Cacheable(cacheNames = "service-cache", key = "#root.methodName")
    public Map<String, Map<String, String>> load() {
        List<SysDictionary> list = sysDictionaryMapper.findAll();
        list.stream().forEach(d -> {
            Map<String, String> item = null;
            if (!data.containsKey(d.getTypeName())) {
                item = new HashMap<>();
                data.put(d.getTypeName(), item);//将该类型的集合存入data
            }//typename是否存在
            else {
                item = data.get(d.getTypeName());

            }
            item.put(d.getCode(), d.getName());
        });
        //查询分类信息
        List<SysCity> citys = sysCityMapper.selectList(null);
        citys.stream().forEach(c -> {
            Map<String, String> item = null;
            if (!data.containsKey("syscity")) {
                item = new HashMap<>();
                data.put("syscity", item);

            } else {
                item = data.get("syscity");
            }
            item.put(c.getClassId() + "", c.getClassName());
        });
//        redisTemplate.opsForValue().set("service-cache:load",data);
        return data;
    }

    @Override
    public Map<String, Map<String, String>> getData() {
        return data;
    }

    public Map<String, Map<String, String>> setData(Map<String, Map<String, String>> data) {
        return data;
    }

    @Override
    public String getValue(String typeName, String key, String defaultValue) {
        return getValues(typeName).containsKey(key) ? getValues(typeName).get(key) : "";
    }

    @Override
    public Map<String, String> getValues(String typeName) {
        return data.get(typeName);
    }

}
